/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package syncmellariusnote;

import syncmellariusnote.helpers.ConnectorConfigurator;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Vitor
 */
public class InterfaceConnector {

    final char noteNoBd = 'O'; 
    final char syncBds  = 'G';
    final char end  = 'F';
    
    Socket conn;
    SyncInterface RetriveDataBase;
    SyncInterface bdActualization;
    
    public InterfaceConnector() {
        conn = ConnectorConfigurator.getClientConnection();
        RetriveDataBase = new Sequencer();
        bdActualization = new Sequencer();
        if(SyncMellariusNot.getConnectionType()){
            indetifyMessages();
        }else{
            indetifyMessages();
        }
        
    }

    private void indetifyMessages() {
        DataInputStream dIn = null;
        try {
            dIn = new DataInputStream(conn.getInputStream());
            boolean done = true;
            
            while (done) {
               char messageType = dIn.readChar();
                System.out.println(messageType);
                switch (messageType) {
                    case noteNoBd: // Type A
                        System.out.println("Message O: " + dIn.readUTF());
                        RetriveDataBase.retriveDataBase();
                        break;
                    case syncBds: // Type B
                        System.out.println("Message G: " + dIn.readUTF());
                        bdActualization.bdActualization();
                        break;
                    default:
                        done = false;
                }
            }
            dIn.close();
        } catch (IOException ex) {
            Logger.getLogger(InterfaceConnector.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                dIn.close();
            } catch (IOException ex) {
                Logger.getLogger(InterfaceConnector.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

}
